package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Customers;
import cn.springboot.erp.entity.SalesOrders;
import cn.springboot.erp.mapper.CustomersMapper;
import cn.springboot.erp.mapper.SalesOrdersMapper;
import cn.springboot.erp.vo.SaleReturnVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.SalesReturns;
import cn.springboot.erp.service.SalesReturnsService;
import cn.springboot.erp.mapper.SalesReturnsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus
* @description 针对表【sales_returns】的数据库操作Service实现
* @createDate 2024-05-12 10:14:04
*/
@Service
public class SalesReturnsServiceImpl extends ServiceImpl<SalesReturnsMapper, SalesReturns>
    implements SalesReturnsService{

    @Resource
    private SalesOrdersMapper salesOrdersMapper;
    @Resource
    private CustomersMapper customersMapper;

    @Override
    public ResponseResult listPage(QueryParams<SalesReturns> queryParams) {
        Integer page = queryParams.getPage();
        Integer limit = queryParams.getLimit();
        SalesReturns searchParams = queryParams.getSearchParams();

        Page<SalesReturns> salesReturnsPage = new Page<>(page, limit);
        LambdaQueryWrapper<SalesReturns> lambda = new QueryWrapper<SalesReturns>().lambda();
        lambda.eq(ObjectUtils.isNotNull(searchParams.getOrderId()),SalesReturns::getOrderId,searchParams.getOrderId());
        baseMapper.selectPage(salesReturnsPage,lambda);

        List<SaleReturnVO> vo = salesReturnsPage.getRecords().stream().map(saleReturn -> {
            SaleReturnVO saleReturnVO = new SaleReturnVO();
            BeanUtils.copyProperties(saleReturn, saleReturnVO);
            Integer orderId = saleReturn.getOrderId();
            SalesOrders salesOrders = salesOrdersMapper.selectById(orderId);
            saleReturnVO.setOrderName(salesOrders.getOrderNumber());
            saleReturnVO.setTotalAmount(salesOrders.getTotalAmount());
            Integer customerId = salesOrders.getCustomerId();
            Customers customers = customersMapper.selectById(customerId);
            saleReturnVO.setCustomerName(customers.getName());
            return saleReturnVO;
        }).collect(Collectors.toList());

        return ResponseResult.Success()
                .data("total",salesReturnsPage.getTotal())
                .data("vo",vo);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        SalesReturns salesReturns = baseMapper.selectById(id);
        if (salesReturns.getRefundStatus()==0){
            return ResponseResult.Fail().msg("退款中，暂时不可删除");
        }
        baseMapper.deleteById(id);
        return ResponseResult.Success().msg("删除成功");
    }
}




